package com.blog.controller.forntend;


import com.blog.entity.Order;
import com.blog.entity.User;
import com.blog.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author mars123
 * @since 2019-07-02
 */
@Controller
@RequestMapping("/frontend/order")
public class OrderController {

    @Autowired
    private OrderService orderService;


    @RequestMapping("/index")
    public String index(Model model, HttpSession session) {
        User user = (User) session.getAttribute("user");
        model.addAttribute("orders", orderService.getOrdersByUid(user.getId()));
        return "/frontend/order/index";
    }


    @RequestMapping("/create")
    public String create(Integer uid, HttpSession session) {
        Order order = orderService.gengeratOrder(uid, session);
        return "redirect:/frontend/order/view?id=" + order.getId();
    }

    @RequestMapping("/view")
    public String view(Integer id, Model model) {
        model.addAttribute("order", orderService.view(id));
        return "/frontend/order/view";
    }


    @RequestMapping("/pay")
    public String pay(String outTradeNo) {
        int id = orderService.payOrder(outTradeNo).getId();
        return "redirect:/frontend/order/view?id=" + id;
    }


    @RequestMapping("/delete")
    public String delete(HttpSession session, Integer id) {
        orderService.delete(id);
        return "redirect:/frontend/order/index";
    }


}

